<?php

namespace app\admin\service\file;

use tmcore\base\BaseService;
use tmcore\services\upload\Upload as UploadService;
use app\common\enum\FileEnum;
use app\common\model\file\File;
use app\common\model\file\FileCate;
use Exception;

/**
 * 文件服务类
 */
class FileService extends BaseService
{
    /**
     * 获取文件列表
     * @return array
     */
    public static function getFileList(): array
    {
        // 搜索条件
        $searchParam = [
            '=' => ['type', 'cid'],
            '%like%' => ['name'],
            '<=' => ['size'],
            'in' => ['ext@ext']
        ];
        $where = self::getSearchData($searchParam, ['source', '=', FileEnum::SOURCE_ADMIN]);

        // 返回分页数据
        $list = File::page($where, self::getPageData(),['id,cid,type,name,uri,create_time'], ['id'=>'desc']);

        foreach ($list['list'] as &$item) {
            $item['url'] = $item['uri'];
            $item['uri'] = get_file_url($item['uri']);
        }

        return $list;
    }

    /**
     * 获取文件分类列表
     * @return array
     */
    public static function getCateFileList(): array
    {
        // 搜索条件
        $searchParam = [
            '=' => ['type']
        ];
        $where = self::getSearchData($searchParam);
        // 返回分页数据
        $list = FileCate::field(['id,pid,type,name'])
            ->where($where)
            ->select()->toArray();
        //循环分类
        return linear_to_tree($list, 'children');
    }


    /**
     * 移动文件
     * @param $params
     * @return bool
     */
    public static function move($params)
    {
        $res = File::whereIn('id', $params['ids'])
            ->update([
                'cid' => $params['cid']
            ]);
        return $res === false?false:true;
    }

    /**
     * 重命名文件
     * @param $params
     * @return bool
     */
    public static function rename($params)
    {
        return File::edit([
            'name' => $params['name']
        ],$params['id']);
    }

    /**
     * 批量删除文件
     * @param $params
     * @return bool
     */
    public static function delete($params)
    {
        try {
            $result = File::whereIn('id', $params['ids'])->select();
            // 获取上传配置
            $default_upload = com_get_config('default', 'upload');
            // 实例化上传类
            $uploadService = (new UploadService($default_upload));

            foreach ($result as $item) {
                $uploadService->delete($item['uri']);
            }
            File::destroy($params['ids']);

            return true;
        } catch (Exception $e) {
            return self::setError($e->getMessage());
        }

    }

    /**
     * 添加文件分类
     * @param $params
     * @return bool
     */
    public static function addCate($params)
    {
        return FileCate::add([
                'type' => $params['type'],
                'pid' => $params['pid'],
                'name' => $params['name']
            ]);
    }

    /**
     * 编辑文件分类
     * @param $params
     * @return bool
     */
    public static function editCate($params)
    {
        return FileCate::edit([
            'name' => $params['name']
        ],$params['id']);
    }

    /**
     * 删除文件分类
     * @param $params
     * @return bool
     */
    public static function delCate($params)
    {
        return FileCate::del($params['id']);
    }
}